<?php

namespace origin;

/**
 * 请求类
 * @author dece <decezz@qq.com>
 */
class Request
{

    public $get     = [];
    public $post    = [];
    public $input   = [];
    public $request = [];
    public $header  = [];
    public $server  = [];
    public $cookie  = [];
    public $file    = [];

    /**
     * 构造
     * @access public
     */
    public function __construct()
    {
        $this->input = file_get_contents('php://input');
        $this->getHeader();
        $this->_init();
    }

    /**
     * 获取请求header
     * @access protected
     */
    protected function getHeader()
    {
        if (function_exists('apache_request_headers') && $result = apache_request_headers()) {
            $header = $result;
        } else {
            $header = [];
            $server = $_SERVER;
            foreach ($server as $key => $val) {
                if (0 === strpos($key, 'HTTP_')) {
                    $key          = str_replace('_', '-', strtolower(substr($key, 5)));
                    $header[$key] = $val;
                }
            }
            if (isset($server['CONTENT_TYPE'])) {
                $header['content-type'] = $server['CONTENT_TYPE'];
            }
            if (isset($server['CONTENT_LENGTH'])) {
                $header['content-length'] = $server['CONTENT_LENGTH'];
            }
        }
        $this->header = array_change_key_case($header);
    }

    /**
     * 设置或者获取当前的Header
     * @access public
     * @param  string $name header名称
     * @param  string $default 默认值
     * @return string|array
     */
    public function header(string $name = '', string $default = null)
    {
        if ('' === $name) {
            return $this->header;
        }

        $name = str_replace('_', '-', strtolower($name));

        return $this->header[$name] ?? $default;
    }

    /**
     * 当前请求 HTTP_CONTENT_TYPE
     * @access public
     * @return string
     */
    public function contentType()
    {
        $contentType = $this->header('Content-Type');

        if ($contentType) {
            if (strpos($contentType, ';')) {
                [$type] = explode(';', $contentType);
            } else {
                $type = $contentType;
            }
            return trim($type);
        }

        return '';
    }

    /**
     * 获取input
     * @access protected
     * @return array
     */
    protected function getInputData($content)
    {
        $contentType = $this->contentType();
        if ('application/x-www-form-urlencoded' == $contentType) {
            parse_str($content, $data);
            return $data;
        } elseif (false !== strpos($contentType, 'json')) {
            return (array) json_decode($content, true);
        }

        return [];
    }

    /**
     * 获取请求数据
     * @access private
     * @return this
     */
    private function _init()
    {
        $inputData      = $this->getInputData($this->input);
        $this->server   = $_SERVER;
        $this->get      = $_GET;
        $this->post     = $_POST ?: $inputData;
        $this->request  = $_REQUEST;
        $this->cookie   = $_COOKIE;
        $this->file     = $_FILES;
        return $this;
    }

}